#include<string>
#include <windows.h>
#include"Battle.h"
#include "ColorSystem.h"
#include "text_utils.h"

using namespace std;

// 战斗系统

int Battle::getBackupLife() {
    return this->backupLife;
}

int Battle::getBackupMana() {
    return this->backupMana;
}

bool Battle::processPreTurnEffects(Character* character) {
    if (!character->isAlive()) return false;
    return character->processStatusEffects();
}
    
void Battle::displayBattleStatus() {
    if (combatant1->isAlive()) combatant1->displayStatus();
    if (combatant2->isAlive()) combatant2->displayStatus();
}

bool Battle::checkBattleEnd() {
    if (!combatant1->isAlive()) {
        winner = combatant2;
        loser = combatant1;
        isActive = false;
        return true;
    }
    if (!combatant2->isAlive()) {
        winner = combatant1;
        loser = combatant2;
        isActive = false;
        return true;
    }
    return false;
}

/*
void Battle::startBattle() {
    if (isActive) return;  // 防止重复开始战斗
    isActive = true;
    cout << "\n=== 战斗开始: " << this->combatant1->getName() << " vs " << combatant2->getName() << " ===" << endl;
    bool playerTurn = (this->combatant1->getAgility() >= combatant2->getAgility());
    while (isActive) {
        displayBattleStatus();
        if (playerTurn) {
            cout << "\n----- " << this->combatant1->getName() << "的回合 -----" << endl;
            if (!processPreTurnEffects(combatant1)) break;
            if (checkBattleEnd()) break;
            combatant1->chooseAction(combatant2);
        }
        else {
            cout << "\n----- " << combatant2->getName() << "的回合 -----" << endl;
            if (!processPreTurnEffects(combatant2)) break;
            if (checkBattleEnd()) break;
            combatant2->chooseAction(combatant1);
        }
        if (checkBattleEnd()) break;
        playerTurn = !playerTurn;
        cout << "\n按回车键继续...";
        cin.ignore();
        cin.get();
    }
    cout << "\n=== 战斗结束 ===" << endl;
    if (winner == combatant1) {
        cout << "你成功击败了" << combatant2->getName() << "！" << endl;
        this->combatant1->setExpoints(combatant2->getExpReward());
        this->combatant1->heal(10);
        this->combatant1->restoreMana(5);
    }
    else{
        cout << "你被" << this->combatant2->getName() << "击败了..." << endl;
        cout << "是否选择复活（Y or N）：";
        char ans = 'Y';
        cin >> ans;
        if (ans == 'Y' || ans == 'y') {
            this->combatant1->Respawn(this->backupLife, this->backupMana);
        }
        else {
            cout << "Game Over" << endl;
            cout << "菜!就多练(～o￣3￣)～" << endl;
        }
    }
}
*/

void Battle::startBattle() {
    if (isActive) return;  // 防止重复开始战斗
    isActive = true;

    // 使用颜色和延迟打印美化战斗开始提示
    ColorSystem::changeTextColor('R');
    TextUtils::printWithDelay("\n=== 战斗开始: " + combatant1->getName() + " vs " + combatant2->getName() + " ===", 50);
    ColorSystem::changeTextColor('W');

    // 先手判定并添加提示
    bool playerTurn = (combatant1->getAgility() >= combatant2->getAgility());
    if (playerTurn) {
        TextUtils::printWithDelay("\n" + combatant1->getName() + " 先手！", 50);
    }
    else {
        TextUtils::printWithDelay("\n" + combatant2->getName() + " 先手！", 50);
    }
    TextUtils::pressAnyKeyToContinue();
    TextUtils::clearScreen();

    while (isActive) {
        ColorSystem::changeTextColor('W');
        displayBattleStatus();  // 显示战斗状态

        if (playerTurn) {
            ColorSystem::changeTextColor('Q');  // 玩家回合用青色
            TextUtils::printWithDelay("\n----- " + combatant1->getName() + "的回合 -----", 50);
        }
        else {
            ColorSystem::changeTextColor('R');  // 敌人回合用红色
            TextUtils::printWithDelay("\n----- " + combatant2->getName() + "的回合 -----", 50);
        }

        // 处理回合前效果
        if (playerTurn) {
            if (!processPreTurnEffects(combatant1)) break;
        }
        else {
            if (!processPreTurnEffects(combatant2)) break;
        }

        if (checkBattleEnd()) break;  // 检查战斗是否结束

        // 执行行动
        if (playerTurn) {
            combatant1->chooseAction(combatant2);
        }
        else {
            combatant2->chooseAction(combatant1);
        }

        if (checkBattleEnd()) break;  // 检查战斗是否结束
        playerTurn = !playerTurn;     // 切换回合
        TextUtils::pressAnyKeyToContinue();  // 统一的继续提示
        TextUtils::clearScreen();
    }

    // 战斗结束处理
    ColorSystem::changeTextColor('R');
    TextUtils::printWithDelay("\n=== 战斗结束 ===", 50);
    Sleep(3000);
    TextUtils::clearScreen();

    if (winner == combatant1) {
        ColorSystem::changeTextColor('G');  // 胜利用绿色
        TextUtils::printWithDelay("你成功击败了" + combatant2->getName() + "！", 50);
        combatant1->setExpoints(combatant2->getExpReward());
        combatant1->heal(10);
        combatant1->restoreMana(5);
        TextUtils::pressAnyKeyToContinue();  // 统一的继续提示
        TextUtils::clearScreen();
    }
    else {
        ColorSystem::changeTextColor('R');  // 失败用红色
        TextUtils::printWithDelay("你被" + combatant2->getName() + "击败了...", 50);
        cout << "是否选择复活（Y or N）：";
        char ans = 'Y';
        cin >> ans;
        if (ans == 'Y' || ans == 'y') {
            combatant1->Respawn(backupLife, backupMana);
            ColorSystem::changeTextColor('G');
            TextUtils::printWithDelay("已复活！", 50);
        }
        else {
            TextUtils::printWithDelay("Game Over", 50);
            TextUtils::printWithDelay("菜!就多练(～o￣3￣)～", 50);
        }
    }
    ColorSystem::changeTextColor('W');
}


bool Battle::isBattleActive() const { 
    return this->isActive; 
}
   
Character* Battle::getWinner() const { 
    return this->winner; 
}
    
Character* Battle::getLoser() const {
    return this->loser; 
}
